package net.easyjava.test.henanxiqudaquan;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import net.easyjava.third.db.mysql.MysqlClient;
import net.easyjava.third.letv.LetvVideoClient;
import net.easyjava.third.letv.bean.LetvVideo;
import net.easyjava.tools.http.HttpTools;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ludi
 * @Date 2016 06 27 下午12:41
 */
public class GetData {

	//获取所有的趣单
	public static final String INIT ="http://waqu.com/v4/user/qudanSubscribed.json?platform=and&appName=s_henanxiqu&profile=general_and&sid=864516020247995&partner=1000&appVersion=3.5.7&start=0&size=200";
	//获取该趣单下所有的视频
	public static final String QUDAN_VIDEO_LIST = "http://waqu.com/v4/qudan/videos.json?platform=and&appName=s_henanxiqu&profile=general_and&sid=864516020247995&partner=1000&appVersion=3.5.7&qdid=";

	public static final String  jdbcUrl = "jdbc:mysql://mingjuer.mysql.rds.aliyuncs.com:3306/crawler";
	public static final String  jdbcName = "juer";
	public static final String  jdbcPassword = "Juer2016";

	public static void get() {
		MysqlClient client = null;
		try {
			client = MysqlClient.getInstance(jdbcUrl, jdbcName,jdbcPassword);
			String init = HttpTools.get(INIT);
			JSONObject json = JSONObject.parseObject(init);
			JSONArray qudans = json.getJSONArray("qudans");
			for (Object qudan : qudans) {
				JSONObject jo = (JSONObject) qudan;
				//保存趣单
				QuDanBean quDanBean = JSONObject.parseObject(jo.toString(), QuDanBean.class);
				saveQuDan(client, quDanBean);
				String url = QUDAN_VIDEO_LIST + jo.get("id") + "&isMakeQudan=false&size=1000";
				String s1 = HttpTools.get(url);
				JSONObject jsonObject = JSONObject.parseObject(s1);
				//获取视频集合json
				JSONArray videos = jsonObject.getJSONArray("videos");
				System.out.println(videos.size());

				for (Object video : videos) {//遍历,获取每个视频的信息
					JSONObject videoJson = (JSONObject) video;
					QuDanVideoBean quDanVideoBean = JSONObject.parseObject(videoJson.toString(), QuDanVideoBean.class);
					quDanVideoBean.setQdid(quDanBean.getId());
					System.out.println(quDanVideoBean.getTitle());
					saveVideo(client, quDanVideoBean);
				}
			}
			//获取所有的趣单id
			JSONArray hotQudans = json.getJSONArray("hotQudans");
			System.out.println(hotQudans.size());
			for (Object qudan : hotQudans) {
				JSONObject qudanJson = (JSONObject) qudan;
				//保存趣单
				QuDanBean quDanBean = JSONObject.parseObject(qudanJson.toString(), QuDanBean.class);
				saveQuDan(client, quDanBean);
				//根据id,拼接URL,获取趣单包含的视频
				String videoUrl = QUDAN_VIDEO_LIST + qudanJson.get("id") + "&isMakeQudan=false&size=1000";
//				System.out.println(videoUrl);
				String s1 = HttpTools.get(videoUrl);
				JSONObject jsonObject = JSONObject.parseObject(s1);
				//获取视频集合json
				JSONArray videos = jsonObject.getJSONArray("videos");
				System.out.println(videos.size());

				for (Object video : videos) {//遍历,获取每个视频的信息
					JSONObject videoJson = (JSONObject) video;
					QuDanVideoBean quDanVideoBean = JSONObject.parseObject(videoJson.toString(), QuDanVideoBean.class);
					quDanVideoBean.setQdid(quDanBean.getId());
					System.out.println(quDanVideoBean.getTitle());
					boolean flag = saveVideo(client, quDanVideoBean);
					if (!flag) {
						updateVideo(client, quDanVideoBean);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.destroy();
		}
	}

	private static void updateVideo(MysqlClient client, QuDanVideoBean quDanVideoBean) {
		String sql = "UPDATE HNXQDQ_QUDAN_VIDEO SET qdid = CONCAT(qdid,',',?) WHERE id = ?";
		try {
			client.execute(sql, quDanVideoBean.getQdid(),quDanVideoBean.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static boolean saveVideo(MysqlClient mysqlClient,QuDanVideoBean quDanVideoBean){
		String sql = "INSERT INTO HNXQDQ_QUDAN_VIDEO(id,title,duration,video_size,flash_url,preview,big_image_url," +
				"img_url,video_url,download_url,qdid) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		boolean flag = false;
		try {
			flag = mysqlClient.execute(sql, quDanVideoBean.getId(), quDanVideoBean.getTitle(), quDanVideoBean.getDuration(),
                    quDanVideoBean.getVideoSize(), quDanVideoBean.getFlashUrl(), quDanVideoBean.getPreview(),
                    quDanVideoBean.getBigImageUrl(), quDanVideoBean.getImageUrl(), quDanVideoBean.getVideoUrl(), quDanVideoBean.getDownloadUrl(),quDanVideoBean.getQdid());
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return flag;
	}

	public static boolean saveQuDan(MysqlClient mysqlClient,QuDanBean quDanBean){
		String sql = "INSERT INTO HNXQDQ_QUDAN(id,name,total) VALUES(?,?,?)";
		boolean flag = false;
		try {
			flag = mysqlClient.execute(sql, quDanBean.getId(), quDanBean.getName(), quDanBean.getTotal());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public static List<JSONObject> getDownloadUrl(MysqlClient client) {
		String sql = "SELECT id,download_url FROM HNXQDQ_QUDAN_VIDEO WHERE is_upload = 0 ORDER BY duration ASC";
		try {
			return client.query(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}

	public static void uploadLetv() {
		LetvVideoClient letvVideoClient = LetvVideoClient.getInstance("eitmmd98tw", "af7976083218b2e1ecb99772b3b34b11");
		MysqlClient client = null;
		try {
			client = MysqlClient.getInstance(jdbcUrl, jdbcName, jdbcPassword);
			List<JSONObject> downloadUrls = getDownloadUrl(client);
//			int j = 20;
//			int i = downloadUrls.size()/j;
//			for (int k = 0 ;k<j ;k++) {
//				List<JSONObject> urls = null;
//				if (k == (j - 1)) {
//					urls = downloadUrls.subList(i * k, downloadUrls.size());
//					UploadVideoThread uvt = new UploadVideoThread(urls);
//					Thread t = new Thread(uvt);
//					t.start();
//					continue;
//				}
//				urls = downloadUrls.subList(i*k,i*j);
//				UploadVideoThread uvt = new UploadVideoThread(urls);
//				Thread t = new Thread(uvt);
//				t.start();
//			}
			for (JSONObject o : downloadUrls) {
				String downloadUrl = (String) o.get("download_url");
				String id = (String) o.get("id");
				String s = HttpTools.get(downloadUrl);
				JSONObject jsonObject = JSONObject.parseObject(s);
				String uploadUrl = (String) jsonObject.get("url");
				LetvVideo letvVideo = letvVideoClient.uploadVideoByUrl(uploadUrl);
				updateVideo(client, letvVideo, id);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.destroy();
		}

	}

	private static void updateVideo(MysqlClient client, LetvVideo letvVideo, String id) {
		String sql = "UPDATE HNXQDQ_QUDAN_VIDEO SET is_upload = 1,video_id=?,video_unique=? WHERE id=?";
		try {
			client.execute(sql, letvVideo.getVideoId(), letvVideo.getVideoUnique(), id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	private static void getVideo() {

	}


}
